<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/9/14
 * Time: 14:33
 */

namespace Home\Controller;
use Think\Controller;
use Think\Model;
use Home\Model\UserModel;

class LoginController extends BaseController {

    /*
     * 提交用户的信息判断是否存在进行登录
     * @param string $mobile //手机号码
     * @param string $password //登录密码
     * return //返回登录结果
     */
    public function login()
    {
        $model = new Model();
       $mobile = I('get.mobile');
        $password = I('get.password');
        $password = md5($password);
        $sql = "select a.id,a.statu,a.type,b.sex from wl_user a left join wl_userinfo b on a.id=b.user_id where a.user=$mobile and a.password='$password' ";
        $data = $model->query($sql);
        if(!empty($data[0]))
        {
            $condition['user'] = $mobile;
            $password['password'] = $password;
            $Model = M('user');
            $result = $data[0];
            $update['login_time'] = date('Y-m-d H:i:s',time());
            $Model->where($condition)->save($update);
        }
        else{
            $result = array(
                'msg'=>'user is not exist',
                'error_code'=>404
            );
        }
        echo json_encode($result,JSON_UNESCAPED_UNICODE);

    }

    /*
     * 调用发送短信接口下发短信
     * @param $mobile //手机号码
     * return 短信发送状态信息
     */
    public function send_sms()
    {
        $model = M('user');
        $mobile = I('get.mobile');
        //判断手机号是否注册过
        $condition['user'] = $mobile;
        $is_mobile = $model->where($condition)->select();
        if(!empty($is_mobile))
        {
            $data = array(
                'msg'=>'mobile is exist',
                'status'=>3
            );
        }
        else {
            $res = R('sms/send_sms', array($mobile,1));
            if (!empty($res)) {
                $data = array(
                    'msg' => 'send sms success',
                    'status' => 1,
                    'code' => $res,
                );
            } else {
                $data = array(
                    'msg' => 'send sms failed',
                    'status' => 2
                );
            }
        }
            $res = json_encode($data, JSON_UNESCAPED_UNICODE);
            echo $res;
    }

    /*
     * 找回密码
     * @param string $mobile //手机号码
     * @param int $type //短信类型，1为注册验证码，2为找回密码验证短信
     * return 短信发送状态信息
     */
    public function back_password()
    {
        $mobile = I('get.mobile');
        $type = I('get.type');
        $res = R('sms/send_sms',array($mobile,$type));
        if(!empty($res))
        {
            $data = array(
                'msg' => 'send sms success',
                'status' => 1,
                'code' => $res,
            );
        }else{
            $data = array(
                'msg' => 'send sms failed',
                'status' => 2
            );
        }
        echo json_encode($data,JSON_UNESCAPED_UNICODE);
    }

    /*
     * 验证码找回密码
     * @param string $mobile //手机号码
     * @param string $password //密码
     * @param string $code //验证码
     * return 返回找回密码状态信息
     */
    public function get_password()
    {
        $model = M('user');
        $mobile = I('post.mobile');
        $password = I('post.password');
        $code = I('post.code');
        $check = $this->check_code($mobile,$code,2);
        if($check)
        {
           $condition['user'] = $mobile;
            $data['password'] = md5($password);
            $result = $model->where($condition)->save($data);
            if(!empty($result))
            {
                $back = array(
                    'msg'=>'password back success!',
                    'statu'=>1
                );
            }else{
                $back = array(
                    'msg'=>'password back failed',
                    'statu'=>3
                );
            }
        }else{
            $back = array(
                'msg'=>'code is not correct!',
                'statu'=>2,
            );
        }
        echo json_encode($back,JSON_UNESCAPED_UNICODE);
    }



    /*
     * 验证短信是否正确，正确则进行注册，失败返回错误信息
     * @param string $mobile //手机号码
     * @param string $password //密码
     * @param string $code //短信验证码
     * @param string $check //对短信验证码进行验证的结果，成功则进行下一步
     */
    public function register()
    {
        $model = M('user');
        $mobile = I('get.mobile');
        $password = I('get.password');
        $code = I('get.code');
        $invite_code = I('get.invite_code');
        //检查邀请码是否正确
        if(!empty($invite_code))
        {
            $check_invite = $this->check_invite_code($invite_code);
            if(empty($check_invite))
            {
                $result = array(
                    'msg'=>'invite code is not correct!',
                    'statu'=>5,
                );
                echo json_encode($result);
                exit;
            }
        }

        $check = $this->check_code($mobile,$code,1);
        if($check)
        {
            $data = array(
                'user' => $mobile,
                'password' => md5($password),
                'invite_code'=>!empty($invite_code)?$invite_code:'',//如果没有输入邀请码，则为空
                'invite_type'=>!empty($invite_code)?1:2,
                'my_code'=>parent::createCode(),
                'register_time'=>date('Y-m-d H:i:s',time()),
            );
            $res = $model->data($data)->add();
            if (!empty($res)) {
                $this->add_userid($res);
                $result = array(
                    'id'=>$res,
                    'msg' => 'register success',
                    'statu' => 1,
                );
            } else {
                $result = array(
                    'msg' => 'register failed',
                    'statu' => 2
                );
            }
            echo json_encode($result,JSON_UNESCAPED_UNICODE);
        }else{
            $result =array(
                'msg'=>'code is not correct',
                'statu'=>3
            );
            echo json_encode($result,JSON_UNESCAPED_UNICODE);
        }
    }


    /*
     * 注册成功的时候将userinfo表写入用户user_id
     * @param int $userid //用户id
     * @param array $data //写入userinfo表中的数据
     * return 无返回结果
     */
    public function add_userid($userid)
    {
        $uid =rand(1000,9999);
        $model = M('userinfo');
        $data['user_id'] = $userid;
        $data['uid'] = $uid.$userid;
        $model->data($data)->add();

    }

    /*
     * 检查验证码是否正确
     * @param string $mobile //手机号码
     * @param string $code //短信验证码
     * 如果正确更新短信验证码的状态为2已使用
     * return true or false
     */
    public function check_code($mobile,$code,$type)
    {
        $model = M('sms');
        $condition['mobile'] = $mobile;
        $condition['code'] = $code;
        $condition['statu'] = 1;
        $condition['type'] = $type;
        $data = $model->where($condition)->select();
        if(!empty($data[0]))
        {
            $res['statu'] = 2;
            $model->where($condition)->save($res);
            return true;
        }else{
            return false;
        }
    }

    //检查邀请码是否正确
    public function check_invite_code($invite_code)
    {
        $model = M('user');
        $condition['my_code'] = $invite_code;
        $res = $model->where($condition)->select();
        if(!empty($res))
        {
            return true;
        }else{
            return false;
        }
    }

    /*
     * 注销账号,此接口可删除账号的所有信息，慎用
     * @param int $id //用户id
     * @return 返回删除状态信息
     */
    public function delete_acount()
    {
        $id = I('get.id');
        $model = M('user');
        $condition['id'] = $id;
        $res = $model->where($condition)->delete();
        $Model = M('userinfo');
        $Condition['user_id'] = $id;
        $result = $Model->where($Condition)->delete();
        if(!empty($res)&&!empty($result))
        {
            $data = array(
                'msg'=>'delete acount success',
                'statu'=>1
            );
        }else{
            $data = array(
                'msg'=>'delete acount failed',
                'statu'=>2
            );
        }
        $Model1 = M('images');
        $Model1->where($Condition)->delete();
        $delete = new UserModel();
        $delete->delete_account($id);
        echo json_encode($data);
    }


}